#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

void print_arr(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

Swap(char* p1, char* p2, size_t width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char temp = *p2;
		*p2 = *p1;
		*p1 = temp;
		p1++;
		p2++;
	}
}


void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			//比较arr[j] 和 arr[j + 1]
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
			{
				Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
			}
		}
	}
}






int cmp_int(const* p1, const* p2)
{
	if ((*(int*)p1) > (*(int*)p2))
		return 1;
	else if ((*(int*)p1) == (*(int*)p2))
		return 0;
	else
		return -1;
}

void test()
{
	int arr[] = { 10,9,8,7,6,5,4,3,2,1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
	print_arr(arr, sz);
}





int main()
{
	test();
	return 0;
}